package rpt.yto.flex.remote.data.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import rpt.yto.flex.remote.data.bean.RPTTableBBean;
import rpt.yto.flex.remote.data.dao.ITableDAO;
import rpt.yto.util.beans.SuperBean;

@SuppressWarnings({"unchecked" , "rawtypes"})
public class TableDAO extends SqlMapClientDaoSupport implements ITableDAO {

	
	@Override
	public List<RPTTableBBean> queryTableByCode(Map<String, Object> param)
			throws Exception {
		
		return getSqlMapClientTemplate().queryForList("selTableBByCode", param);
	}

	@Override
	public Object insertHeadData(SuperBean bean) throws Exception {
		
		Object genKey = getSqlMapClientTemplate().insert("insertHeadData", bean);
		
		return genKey;
	}

	@Override
	public String insertBodyData(List<SuperBean> beanList , String sqlMapType) throws Exception {
		
		String pk_statistic = null;
		for(SuperBean bean : beanList) {
			
			try {
				getSqlMapClientTemplate().insert(sqlMapType , bean);
			} catch(Exception e) {
				e.printStackTrace();
				continue;
			}
			
			pk_statistic = (String) bean.getAttributeValue("pk_statistic");
		}
		
		return pk_statistic;
	}
	
	@Override
	public List<SuperBean> queryBeanByCondition(Map<String , Object> paramMap, Class beanType) throws Exception {
		
		List<Map<String , Object>> resultMap = getSqlMapClientTemplate().queryForList("queryStatisticData" , paramMap);
		List<SuperBean> beanList = new ArrayList<SuperBean>();
		
		if(resultMap != null && resultMap.size() > 0) {
			for(Map<String , Object> beanMap : resultMap) {
				beanList.add(SuperBean.getMapToBean(beanMap, beanType));
			}
		}
		
		return beanList;
		
	}

	@Override
	public String deleteStatisticData(Map<String, Object> paramMap) throws Exception {
		return String.valueOf(getSqlMapClientTemplate().update("deleteStatisticData", paramMap));
		
	}

	@Override
	public String insertHeadTable(Map<String, String> param) throws Exception {
		return String.valueOf(getSqlMapClientTemplate().insert("insertHeadTable", param));
	}

	@Override
	public String insertBodyTable(Map<String, String> param) throws Exception {
		return String.valueOf(getSqlMapClientTemplate().insert("insertBodyTable", param));
	}

	@Override
	public String queryExsits(String reportcode) throws Exception {
		return (String) getSqlMapClientTemplate().queryForObject("queryExsitsRpt", reportcode);
	}

	@Override
	public List<SuperBean> queryTableBByPK(String pk_table)
			throws Exception {
		
		List<Map<String , Object>> resultMap = getSqlMapClientTemplate().queryForList("queryTableBByPK", pk_table);

		List<SuperBean> beanList = new ArrayList<SuperBean>();
		
		if(resultMap != null && resultMap.size() > 0) {
			for(Map<String , Object> beanMap : resultMap) {
				beanList.add(SuperBean.getMapToBean(beanMap, RPTTableBBean.class));
			}
		}
		
		return beanList;
		
	}

	@Override
	public String deleteBodyTable(String reportcode) throws Exception {
		
		getSqlMapClientTemplate().delete("deleteBodyTable", reportcode);
		
		return "true";
	}

}
